package com.engine.cese.duss.srm.chat.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.cese.duss.srm.util.Base64Util;
import com.engine.cese.duss.srm.util.CalUtil;
import com.engine.cese.duss.srm.util.DataUtil;
import com.engine.cese.duss.srm.util.GenUtil;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-09
 * @Description: 大厅聊天记录表
 * @ModifyLog:
 **/

public class WeaChatAction {

    /**
     * @Description: 插入聊天记录
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/7/6
     */
    @GET
    @Path("/insertChatRecord")
    @Produces({MediaType.TEXT_PLAIN})
    public String insertChatRecord(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            RecordSet rs = new RecordSet();
            String cglxdBase64 = Util.null2String( request.getParameter( "cglxdBase64" ) );
            rs.writeLog( "cglxdBase64="+cglxdBase64 );
            if (!"".equals( cglxdBase64 )) {
                String cglxd = Base64Util.decode( cglxdBase64 );
                rs.writeLog( "cglxd="+cglxd );
                User user = HrmUserVarify.getUser(request, response);
                String loginid = user.getLoginid();
                String logintype = user.getLogintype();
                String username = user.getUsername();
                String curdate = DateUtil.getCurrentDate();
                String sql = "select cglxd from uf_chatrecord where cglxd=? and loginid=? and substr(datetime,0,10)=?";
                rs.executeQuery( sql, cglxd, loginid, curdate );
                if (!rs.next()) {
                    String datetime = DateUtil.getFullDate();
                    UUID REC_UUID = UUID.randomUUID();
                    String uuid = REC_UUID.toString();
                    String sql1 = "insert into uf_chatrecord(cglxd,uuid,loginid,logintype,username,datetime) values (?,?,?,?,?,?)";
                    Object[] params = {cglxd, uuid, loginid, logintype, username, datetime};
                    rs.executeUpdate( sql1, params );
                }
                apidatas.put( "api_status", true );
            }

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 插入聊天内容
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/insertChatContent")
    @Produces({MediaType.TEXT_PLAIN})
    public String insertChatContent(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            RecordSet rs = new RecordSet();
            String cglxdBase64 = Util.null2String( request.getParameter( "cglxdBase64" ) );
            rs.writeLog( "cglxdBase64="+cglxdBase64 );
            if (!"".equals( cglxdBase64 )) {
                String cglxd = Base64Util.decode( cglxdBase64 );
                rs.writeLog( "cglxd="+cglxd );
                User user = HrmUserVarify.getUser( request, response );
                String loginid = user.getLoginid();
                String content = Util.null2String( request.getParameter( "content" ) );
                String curdate = DateUtil.getFullDate();
                UUID CON_UUID = UUID.randomUUID();
                String uuid = CON_UUID.toString();
                String sql = "insert into uf_chatcontent(cglxd,uuid,loginid,chatcontent,chatdateime) values(?,?,?,?,?)";
                Object[] params = {cglxd, uuid, loginid, content, curdate};
                rs.executeUpdate( sql, params );
                apidatas.put( "api_status", true );
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 根据采购立项、日期获取主持人和供应商的聊天内容
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/queryHostChat")
    @Produces({MediaType.TEXT_PLAIN})
    public String queryHostChat(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            RecordSet rs = new RecordSet();
            String cglxdBase64 = Util.null2String( request.getParameter( "cglxdBase64" ) );
            rs.writeLog( "cglxdBase64="+cglxdBase64 );
            if (!"".equals( cglxdBase64 )) {
                String cglxd = Base64Util.decode( cglxdBase64 );
                rs.writeLog( "cglxd="+cglxd );
                JSONArray dataList = new JSONArray();
                String sql2 = "select t1.loginid,t1.logintype,t1.username,t.chatcontent,t.chatdateime from uf_chatcontent t left join uf_chatrecord t1\n" +
                        "on t.cglxd=t1.cglxd and t.loginid=t1.loginid where t1.cglxd=? order by t.chatdateime";
                rs.executeQuery( sql2, cglxd );
                while (rs.next()) {
                    JSONObject jsonObject = new JSONObject();
                    String loginid = Util.null2String( rs.getString( "loginid" ) );
                    String username = Util.null2String( rs.getString( "username" ) );
                    String chatcontent = Util.null2String( rs.getString( "chatcontent" ) );
                    String chatdateime = Util.null2String( rs.getString( "chatdateime" ) );
                    jsonObject.put( "uid", loginid );
                    jsonObject.put( "type", 100 );
                    jsonObject.put( "date", chatdateime );
                    jsonObject.put( "nickname", username );
                    jsonObject.put( "msg", chatcontent );
                    dataList.add( jsonObject );
                }
                apidatas.put( "dataList", dataList );
                apidatas.put( "api_status", true );
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 根据采购立项、日期获取供应商和主持人的聊天内容
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/queryGuestChat")
    @Produces({MediaType.TEXT_PLAIN})
    public String queryGuestChat(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            RecordSet rs = new RecordSet();
            String cglxdBase64 = Util.null2String( request.getParameter( "cglxdBase64" ) );
            rs.writeLog( "cglxdBase64="+cglxdBase64 );
            if (!"".equals( cglxdBase64 )) {
                String cglxd = Base64Util.decode( cglxdBase64 );
                rs.writeLog( "cglxd="+cglxd );
                User user = HrmUserVarify.getUser( request, response );
                String loginid = user.getLoginid();
                JSONArray dataList = new JSONArray();
                String sql2 = "select t1.loginid,t1.logintype,t1.username,t.chatcontent,t.chatdateime from uf_chatcontent t left join uf_chatrecord t1\n" +
                        "on t.cglxd=t1.cglxd and t.loginid=t1.loginid where t.cglxd=? and (t1.loginid=? or t1.logintype=1) order by t.chatdateime";
                rs.executeQuery( sql2, cglxd, loginid );
                while (rs.next()) {
                    JSONObject jsonObject = new JSONObject();
                    String uid = Util.null2String( rs.getString( "loginid" ) );
                    String username = Util.null2String( rs.getString( "username" ) );
                    String chatcontent = Util.null2String( rs.getString( "chatcontent" ) );
                    String chatdateime = Util.null2String( rs.getString( "chatdateime" ) );
                    jsonObject.put( "uid", uid );
                    jsonObject.put( "type", 100 );
                    jsonObject.put( "date", chatdateime );
                    jsonObject.put( "nickname", username );
                    jsonObject.put( "msg", chatcontent );
                    dataList.add( jsonObject );
                }
                apidatas.put( "dataList", dataList );
                apidatas.put( "api_status", true );
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }
}
